package org.dromara.system.service.impl;

import org.dromara.system.service.SysRoleService;
import org.dromara.system.pojo.entity.SysRoleDO;
import org.dromara.system.pojo.bo.SysRoleBO;
import org.dromara.system.pojo.qo.SysRoleQO;
import org.dromara.system.pojo.vo.SysRoleVO;
import org.dromara.system.mapper.SysRoleMapper;
import org.springframework.stereotype.Service;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;

import java.util.List;

/**
 * 角色信息表 服务层实现。
 *
  * @author mkj
  * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysRoleServiceImpl  implements SysRoleService {

    private final SysRoleMapper sysRoleMapper;

    @Override
    public boolean add(SysRoleBO sysRoleBO) {
        SysRoleDO sysRoleDO = BeanCopyUtils.copy(sysRoleBO, SysRoleDO.class);
        return sysRoleMapper.insert(sysRoleDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysRoleMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysRoleBO sysRoleBO) {
        SysRoleDO sysRoleDO = BeanCopyUtils.copy(sysRoleBO, SysRoleDO.class);
        return sysRoleMapper.update(sysRoleDO) > 0;
    }

    @Override
    public List<SysRoleVO> list(SysRoleQO sysRoleQO) {
        SysRoleDO sysRoleDO = BeanCopyUtils.copy(sysRoleQO, SysRoleDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysRoleDO);
        return sysRoleMapper.selectListByQueryAs(queryWrapper, SysRoleVO.class);
    }

    @Override
    public SysRoleVO getInfo(@PathVariable Long id) {
        SysRoleDO sysRoleDO = sysRoleMapper.selectOneById(id);
        if (sysRoleDO == null){
            return null;
        }
        return BeanCopyUtils.copy(sysRoleDO, SysRoleVO.class);
    }

    @Override
    public Page<SysRoleVO> page(SysRoleQO page) {
        return sysRoleMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysRoleVO.class);
    }

}

